package com.zhan.ielts.utils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Zhengze {

	public static void main(String[] args) {
		String str = "How are u? I... am fine! My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country music band. They would play at local dances and on a few occasions would play for the local radio station. He often told us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and cursing the day of his audition and he did not want to be around that type of environment.While having some money does have an impact on our level of happiness, having a lot of money does not. People in the United States whose income goes from US$20,000 a year to US$50,000 a year are more likely to be happy.";
		handle(str);
		// replaceString(str, getParam());
	}

	public static void test(String str) {
		Pattern pattern = Pattern
				.compile("(?!\\s).+?(?:[!?]|$|(?:(?<!\\d)\\.(?!\\d)))");
		Matcher matcher = pattern.matcher(str);
		int count = 0;
		while (matcher.find()) {
			System.out.println("第" + ++count + "句:" + matcher.group(0));
		}

	}

	public static Map handle(String str) {
		System.out.println(str);
		Map map = new HashMap();
		// 替换特使字符
		str = replaceString(str, getParam());

		Pattern pattern = Pattern
				.compile("(?!\\s).+?(?:[!?]|$|(?:(?<!\\d)\\.(?!\\d)))");
		Matcher matcher = pattern.matcher(str);
		int count = 0;
		StringBuffer sb = new StringBuffer("");
		int num = 0;
		while (matcher.find()) {
			System.out.println("第" + ++count + "句:" + matcher.group(0));
			// sb.append(matcher.group(0) + "@@ ");
			if (matcher.group(0).length() < 15) {
				sb.append(matcher.group(0));
			} else {
				sb.append(matcher.group(0) + " @@");
				num++;
			}
		}

		// 替换回来特殊字符
		String resultStr = sb.toString();
		resultStr = replaceString(resultStr, getParam2());
		// resultStr = resultStr.substring(0, resultStr.length()-3);
		resultStr = resultStr.substring(0, resultStr.length());
		System.out.println(resultStr);
		if (num == 0)
			num = 1;
		map.put("num", num);
		map.put("sentence", resultStr);
		return map;

	}

	public static Map handleCn(String str) {
		System.out.println(str);
		Map map = new HashMap();
		// 替换特使字符
		str = replaceString(str, getParam());

		Pattern pattern = Pattern
				.compile("(?!\\s).+?(?:[!?]|$|(?:(?<!\\d)\\.(?!\\d)))");
		Matcher matcher = pattern.matcher(str);
		int count = 0;
		StringBuffer sb = new StringBuffer("");
		int num = 0;
		while (matcher.find()) {
			System.out.println("第" + ++count + "句:" + matcher.group(0));
			if (matcher.group(0).length() < 10) {
				sb.append(matcher.group(0));
			} else {
				sb.append(matcher.group(0) + "@@");
				num++;
			}

			// sb.append(matcher.group(0) + "@@ ");
		}

		// 替换回来特殊字符
		String resultStr = sb.toString();
		// resultStr = resultStr.replaceAll("\\.@@", "。@@");
		// resultStr = resultStr.replaceAll("\\?@@", "？@@");
		// resultStr = resultStr.replaceAll("\\!@@", "！@@");
		resultStr = resultStr.replaceAll("\\.", "。");
		resultStr = resultStr.replaceAll("\\?", "？");
		resultStr = resultStr.replaceAll("\\!", "！");
		resultStr = replaceString(resultStr, getParam2());
		resultStr = resultStr.substring(0, resultStr.length());
		System.out.println(resultStr);

		if (num == 0)
			num = 1;
		map.put("num", num);
		map.put("sentence", resultStr);
		// return resultStr;
		return map;
	}

	public static Map<String, String> getParam() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("A.D.", "#ad#");
		map.put("B.C.", "#bc#");
		map.put("Mr.", "#mr#");
		map.put("Dr.", "#dr#");
		map.put("\\.\\.\\.\\.\\.", "#5d#");
		map.put("\\.\\.\\.\\.", "#4d#");
		map.put("\\.\\.\\.", "#3d#");
		map.put("\\.\\.", "#2d#");

		map.put("。", ".");
		map.put("？", "?");
		map.put("！", "!");
		map.put("\n","#enter#");

		return map;
	}

	public static Map<String, String> getParam2() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		// map.put(".", "。");
		// map.put("?", "？");
		// map.put("!", "！");

		map.put("#ad#", "A.D.");
		map.put("#bc#", "B.C.");
		map.put("#mr#", "Mr.");
		map.put("#dr#", "Dr.");
		map.put("#5d#", ".....");
		map.put("#4d#", "....");
		map.put("#3d#", "...");
		map.put("#2d#", "..");
		map.put("#enter#","\n");
		return map;
	}

	public static String replaceString(String str, Map<String, String> map) {
		for (Map.Entry<String, String> entry : map.entrySet()) {
			// System.out.println("Key = " + entry.getKey() + ", Value = " +
			// entry.getValue());
			str = str.replaceAll(entry.getKey(), entry.getValue());
		}
		// System.out.println(str);
		return str;

	}


	public static String regexTime(String str, boolean isStart) {
		if (str == null) {
			return null;
		}
		String regEx = "\\|\\|(\\d+\\:\\d+\\.\\d+)\\|\\|";
		Pattern pat = Pattern.compile(regEx);
		Matcher mat = pat.matcher(str);
		String last = null;
		int count = 0;
		while (mat.find()) {
			++count;
			if (isStart) {
				return mat.group(1);
			} else {
				last = mat.group(1);
			}
		}
		if (!isStart && last != null && count > 1) {
			return last;
		}
		return null;
	}

}
